package org.dandelion.calendar;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import org.dandelion.R;

public class DateAdapter extends BaseAdapter {
    private int[] days = new int[42];
    private Context context;
    private int year;
    private int month;
    private String signDay[];
    Typeface font;
    private int icon[] = {R.drawable.ic_chibrush, R.drawable.ic_chrope, R.drawable.ic_fan,
            R.drawable.ic_pipa, R.drawable.ic_tuanshan, R.drawable.ic_umbrella, R.drawable.ic_zhujian
    };

    public DateAdapter(Context context, int[][] days, int year, int month, String signDay[]) {
        this.context = context;
        int dayNum = 0;
        //将二维数组转化为一维数组，方便使用
        for (int[] day : days) {
            for (int i : day) {
                this.days[dayNum++] = i;
            }
        }
        this.year = year;
        this.month = month;
        this.signDay = signDay;
        font = Typeface.createFromAsset(context.getAssets(), "maobi.ttf");
    }

    @Override
    public int getCount() {
        return days.length;
    }

    @Override
    public Object getItem(int i) {
        return days[i];
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        String str = "";
        str = str + days[i];
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.date_item, null);
            viewHolder = new ViewHolder();
            viewHolder.date_item = (TextView) view.findViewById(R.id.date_item);
            viewHolder.date_item.setTypeface(font);
            viewHolder.img = view.findViewById(R.id.date_itemimg);
            viewHolder.img2 = view.findViewById(R.id.date_itemimg2);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        if (i < 7 && days[i] > 20) {
//            viewHolder.date_item.setTextColor(Color.rgb(204, 204, 204));//将上个月的和下个月的设置为灰色
        } else if (i > 20 && days[i] < 15) {
//            viewHolder.date_item.setTextColor(Color.rgb(204, 204, 204));
        } else {
            boolean nextLine = true;
            for (int j = 0; j < signDay.length; j++) {
                if (Integer.toString(days[i]).equals(signDay[j])) {
                    viewHolder.img.setImageResource(icon[days[i] % 7]);
                    nextLine = false;
                }
            }
            if (nextLine) {
                viewHolder.img.setImageResource(R.drawable.touming);
            }
            viewHolder.date_item.setText(str);
            viewHolder.date_item.setBackgroundResource(R.drawable.ic_moji);
            if (days[i] == DateUtils.getDay() && month == DateUtils.getMonth() && year == DateUtils.getYear()) {
                viewHolder.date_item.setBackgroundResource(R.drawable.ic_mojired);
            }
//            Log.d("日期",""+DateUtils.getDay());
        }
        return view;
    }

    static class ViewHolder {
        TextView date_item;
        ImageView img;
        ImageView img2;
    }
}

